<template>
    <div class="up">
        <img :src="base64" alt="" />
    </div>
</template>

<script>
import axios from "axios";
export default {
    props: ["file"],
    data() {
        return {
            base64: ""
        };
    },
    created() {
        // 本地预览
        let fileReader = new FileReader();
        fileReader.readAsDataURL(this.file);
        fileReader.onload = e => {
            // console.log(e);
            this.base64 = e.currentTarget.result;
            console.log(this.base64);
        };
        // 创建虚拟表单（异步上传）
        const formData = new FormData();
        // 添加表单项
        formData.append("pic", this.file);
        // axios发送请求
        axios.post("/api/up", formData).then(data => {
            console.log(data.data);
            this.$emit("uppic", data.data.name);
        });
    }
};
</script>

<style lang="less"></style>
